from django.contrib.contenttypes.models import ContentType
from django.db import models

from models import Message, MessageItem, MessageManager

class ModelMessageManager(MessageManager):
    """
    A manager for retrieving messages for a particular model.
    """
    def get_query_set(self):
        ctype = ContentType.objects.get_for_model(self.model)
        return Message.objects.filter(
            messages__content_type__pk=ctype.pk).distinct()
    
    def send(self, to_obj, subject, body):
        message = Message(subject=subject, body=body).save()
        return self.send_message(self.model, to_obj, message)
    
    def retrieve(self):
        return self.get_for_object(self.model)
        